home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / bbsckit / bbscport.c < prev    next >
C/C++ Source or Header  |  1979-12-31  |  5KB  |  162 lines

  1. /*
  2.         bbscport.c
  3.  
  4.         Support routines used by BBSc.c to access the modem port.
  5.                           Written By Mike Kelly
  6.  
  7.         Modified to run under UNIX I/O control by Dave Stanhope.
  8.  
  9.         03/09/83 v1.0   written
  10.         07/08/83 v1.0   Added code to look for modem carrier detect,
  11.                         and exit() if not still connected.
  12. */
  13.  
  14. #include "bbscdef.h"
  15. #include <sgtty.h>
  16. #include <sys/ioctl.h>
  17.  
  18. #define LASTDATE  " 07/08/83 "
  19.  
  20. #define PGMNAME "BBSCPORT "
  21. #define VERSION " 1.0 "
  22.  
  23. portinstat()    /*  returns 1 if no char, 0 if char waiting */
  24.         {
  25.         long count  ;
  26.         ioctl(STDIN, FIONREAD, &count) ; /* see if any previous data  */
  27.         if(count > 0) return(0) ; else return(1) ;
  28.         }
  29.   
  30. char portin()           /* get one byte from the port */
  31.         {
  32.         char byte;
  33.         if(read(STDIN, &byte, 1) != 1) return(CPMEOF) ;
  34.         return(byte & 0x7F);
  35.         }
  36.  
  37. portsin(buf,max)        /* get a line of input max. chars long */
  38. int max ; char *buf ;
  39.         {
  40.         int cnt, byte ; char bytex ;
  41.         cnt = 0;
  42.         byte = FALSE;
  43.         while (++cnt <= max && byte != '\r')
  44.                 {
  45.                 while((byte = portin()) < ' ' || byte > '}')
  46.                         {
  47.                         if (byte == '\r') { break ; } /* carriage return */
  48.                         if (byte == '\b' && cnt > 1)    /* backspace */
  49.                                 {
  50.                                 portout(byte);
  51.                                 portout(' ');
  52.                                 portout(byte);
  53.                                 *buf--; /* backout last char */
  54.                                 cnt--;  /* decrement count too */
  55.                                 }
  56.                         }
  57.                 if (byte != '\r')
  58.                         {
  59.                         *buf++ = byte;
  60.                         }
  61.                 portout(byte);  /* echo good chars only */
  62.                 }
  63.         *buf++  = '\0';                 /* tag \0 on end */
  64.         }
  65.  
  66. portout(byte)           /* send one byte to the port */
  67. char byte;              /* return CTL_K for those times want to check */
  68.         {               /* if the person wants to stop sending        */
  69.         char byte0 ;
  70.         if (portinstat() == 0)                  /* == 0 means char waiting */
  71.                 {
  72.                 if ((byte0 = portin()) == 0x13) /* ctl-S?, then */
  73.                         {                       /*  simulate xon/xoff */
  74.                         portin();               /* gobble up the char */
  75.                         }
  76.                 if (byte0 == CTL_K || byte0 == 'K' || byte0 == 'k')
  77.                         {                       /* look for stop sending key */
  78.                         stop_that = TRUE;       /* if so, set switch on      */
  79.                         }
  80.                 }
  81.         byte0 = byte ; write(STDOUT,&byte0,1) ; /* send the byte */
  82.         return(OK) ;
  83.         }
  84.   
  85. portsout(string)        /* send a string to the port */
  86. char *string ;
  87.         {
  88.         char byte ;
  89.  
  90.         while (byte = (*string++))
  91.                 {
  92.                 portout(byte) ;         /* send one byte at a time */
  93.                 }
  94.         }
  95.  
  96. portlsout(string,len)  /* send a string to the port, pad to length */
  97. char *string ; int len ;
  98.         {
  99.         char byte ;
  100.  
  101.         while (byte = (*string++))
  102.                 {
  103.                 portout(byte) ;         /* send one byte at a time */
  104.                 len-- ;
  105.                 }
  106.         while (len > 0) { portout(' ') ; len-- ; } /* pad with spaces */
  107.         }
  108.  
  109. porttype(tbuf)          /* type a file to the port */
  110. FILE    *tbuf ;
  111.         {
  112.         char byte ;
  113.  
  114.         stop_that = FALSE;      /* reset switch */
  115.         portsout("\r\nType ctl-K or K to skip this\r\n\n");
  116.         while (((byte = getc (tbuf)) != EOF) && (byte != CPMEOF))
  117.                 {
  118.                 if(byte == '\n') portout('\r') ;
  119.                 portout(byte);
  120.                 if (stop_that)           /* received ctl-K or K */
  121.                         {
  122.                         portsout(CRLF);
  123.                         stop_that = FALSE;      /* reset switch */
  124.                         return;                 /* nuf's enough */
  125.                         }
  126.                 }
  127.         }
  128.  
  129. portinit()
  130.         {
  131.         /* set raw mode for this terminal */
  132.         struct sgttyb arg;
  133.         ioctl (STDIN, TIOCGETP, &arg);
  134.         arg.sg_flags |= RAW ;
  135.         arg.sg_flags &= ~ECHO;
  136.         ioctl (STDIN, TIOCSETP, &arg);
  137.         }
  138.  
  139. portrst()
  140.         {
  141.         /* set raw mode for this terminal */
  142.         struct sgttyb arg;
  143.         ioctl (STDIN, TIOCGETP, &arg);
  144.         arg.sg_flags &= ~RAW;
  145.         arg.sg_flags |= ECHO ;
  146.         ioctl (STDIN, TIOCSETP, &arg);
  147.         }
  148.  
  149. char gobble()                           /* gobble up any answer */
  150.         {
  151.         int cnt = 0 ;
  152.         while (cnt++ < 20) portin ;
  153.         }
  154.  
  155. /*      end of program          */
  156. arg);
  157.         }
  158.  
  159. char gobble()                           /* gobble up any answer */
  160.         {
  161.         int cnt = 0 ;
  162.         w